package com.lx.pc.restful.index;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.lx.pc.domain.vo.broadcast.BroadcastVo;
import com.lx.pc.domain.vo.category.CategoryVo;
import com.lx.pc.domain.vo.goodssupply.GoodssupplyVo;
import com.lx.pc.domain.vo.quotationtrend.QuotationtrendVo;
import com.lx.pc.domain.vo.supplydemand.SupplydemandVo;
import com.lx.pc.domain.vo.sysarea.SysareaVo;
import com.lx.pc.domain.vo.tendering.TenderingVo;
import com.lx.pc.restful.BaseController;
import com.lx.pc.service.broadcast.IBroadcastService;
import com.lx.pc.service.category.ICategoryService;
import com.lx.pc.service.goodssupply.IGoodssupplyService;
import com.lx.pc.service.quotationtrend.IQuotationtrendService;
import com.lx.pc.service.supplydemand.ISupplydemandService;
import com.lx.pc.service.sysarea.ISysareaService;
import com.lx.pc.service.tendering.ITenderingService;
import com.lx.pc.utils.OmsPage;

@Controller
public class IndexController extends BaseController{
	
	/**
	 * 类目
	 */
	@Resource(name = "categoryService")
	private ICategoryService categoryService;
	
	/**
	 * 轮播
	 */
	@Resource(name = "broadcastService")
	private IBroadcastService broadcastService;

	/**
	 * 现货
	 * @return
	 */

	@Resource(name="goodssupplyService")
	private IGoodssupplyService goodssupplyService;
	/**
	 * 供求
	 * @return
	 */
	@Resource(name = "supplydemandService")
	private ISupplydemandService supplydemandService;
	
	/**
	 * 招标
	 * @return
	 */
	@Resource(name = "tenderingService")
	private ITenderingService tenderingService;
	
	/**
	 * 行情走势图
	 * @return
	 */
	@Resource(name = "quotationtrendService")
	private IQuotationtrendService quotationtrendService;
	
	/**
	 * 地区
	 */
	@Resource(name = "sysareaService")
	private ISysareaService sysareaService;
	
	@RequestMapping(value = "/index")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		
		ModelAndView mv = getModelAndView("index",request);
		
		/**
		 * 查询所有类目
		 */
		CategoryVo category = new CategoryVo();
		category.setParentId("0");
		category.setActive((short) 1);
		category.setStatus(1);
		OmsPage<CategoryVo> list = categoryService.findByCategory(category,1,5);
		OmsPage<CategoryVo> olist = new OmsPage<>();
		if (list.getResult().size() > 0) {
			for (CategoryVo categoryVo : list.getResult()) {
				category.setParentId(categoryVo.getCategoryId());
				olist = categoryService.findByCategory(category,1,5);
				categoryVo.setListCategory(olist.getResult());
			}
		}
		
		/**
		 * 轮播查询
		 */
		BroadcastVo broadcast = new BroadcastVo();
		OmsPage<BroadcastVo> pagesBroadcast = broadcastService.findByBroadcast(broadcast, 1, 4);
		
		/**
		 * 招标
		 */
		TenderingVo tendering = new TenderingVo();
		OmsPage<TenderingVo> pageTendering = tenderingService.findByTendering(tendering,1,5);
		
		//查询地区
		SysareaVo sysarea = new SysareaVo();
		sysarea.setParentCode("0");
	    List<SysareaVo> sysareaList = sysareaService.findBySysareaList(sysarea);
	    
	    //类目
  		CategoryVo categoryVo = new CategoryVo();
  		categoryVo.setType(1);
  		categoryVo.setActive((short) 1);
  		categoryVo.setStatus(1);
  		OmsPage<CategoryVo> categoryList = categoryService.findByCategory(categoryVo,1,5);
		
		//所有类目结果
		mv.addObject("list",list.getResult());
		//轮播结果
		mv.addObject("broadcastVo",pagesBroadcast.getResult());
		//招标结果
		mv.addObject("tenderingVo",pageTendering.getResult());
		//地区结果
		mv.addObject("sysarea",sysareaList);
		//二级类目
		mv.addObject("category",categoryList.getResult());
		
	
		return mv;
	}
	
	/**
	 * 二级类目
	 * @param v
	 * @param page
	 * @param size
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/twoCategoryList")
    @ResponseBody
    public  String twoCategoryList(CategoryVo v,Integer page,Integer size) throws IOException {
        Map<String,Object> result = new HashMap<String, Object>();
        /**
		 * 查询二级类目
		 */
		CategoryVo categoryVo = new CategoryVo();
		categoryVo.setType(1);
		categoryVo.setActive((short) 1);
		categoryVo.setStatus(1);
		OmsPage<CategoryVo> listVo = categoryService.findByCategory(categoryVo,1,5);
        result.put("categoryVo", listVo.getResult());
        return JSON.toJSONString(result);
    }
	
	
	/**
	 * 首页现货
	 * @param v
	 * @param page
	 * @param size
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/supplyList")
    @ResponseBody
    public  String supplyList(GoodssupplyVo v,Integer page,Integer size) throws IOException {
        Map<String,Object> result = new HashMap<String, Object>();
        OmsPage<GoodssupplyVo> pages = goodssupplyService.findByGoodssupply(v, page, size);
        result.put("GoodssupplyVo", pages.getResult());
        return JSON.toJSONString(result);
    }
	
	/**
	 * 首页供求
	 * @param v
	 * @param page
	 * @param size
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/indexSupplydemandList")
	@ResponseBody
    public  String indexSupplydemandList(SupplydemandVo v,Integer page,Integer size) throws IOException {
        Map<String,Object> result = new HashMap<String, Object>();
        OmsPage<SupplydemandVo> pages = supplydemandService.findBySupplydemand(v, page, size);
        for (SupplydemandVo vo : pages.getResult()) {
        	vo.setCreateTime(vo.getCreateTime().substring(5, 10));
        }
        result.put("supplydemandVo", pages.getResult());
        return JSON.toJSONString(result);
    }
	
	
	
	/**
	 * 行情走势 图形数据
	 * @param v
	 * @param page
	 * @param size
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/quotationtrendList")
	@ResponseBody
    public  String quotationtrendList(QuotationtrendVo v,Integer page,Integer size) throws IOException {
        Map<String,Object> result = new HashMap<String, Object>();
        
        CategoryVo categoryVo = new CategoryVo();
		categoryVo.setType(1);
		categoryVo.setActive((short) 1);
		categoryVo.setStatus(1);
		OmsPage<CategoryVo> listVo = categoryService.findByCategory(categoryVo,1,5);
		if (StringUtils.isEmpty(v.getCategoryId())) {
			v.setCategoryId(listVo.getResult().get(0).getCategoryId());
		}
        List<QuotationtrendVo> list = quotationtrendService.findByQuotationtrendList(v);
        result.put("quotationtrendVo", list);
        return JSON.toJSONString(result);
    }
	
	/**
	 * 行情走势 列表数据
	 * @param v
	 * @param page
	 * @param size
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/quotationtrendNameList")
	@ResponseBody
    public  String quotationtrendNameList(QuotationtrendVo v,Integer page,Integer size) throws IOException {
		
        Map<String,Object> result = new HashMap<String, Object>();
        
        OmsPage<QuotationtrendVo> pages = quotationtrendService.findByQuotationtrend(v, page, size);
        for (QuotationtrendVo vo : pages.getResult()) {
        	vo.setCreateTime(vo.getCreateTime().substring(5, 10));
        }
        result.put("quotationtrendVo", pages.getResult());
        return JSON.toJSONString(result);
    }
}
